package edu.hawaii.duedates;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.cli.HelpFormatter;

/**
 * Parse command line args for DueDates options.
 * 
 * @author John Ancheta 
 *         Daniel Arakaki 
 *         Mari-Lee Flestado
 * @version 1.0
 */
public class CommandLineArgs {
  
  /** Static variable. */
  public static final String uhmoption = "uhm"; 
  /** Static variable. */
  public static final String hsloption = "hsl"; 
  
  /**
   * Parse command line for DueDates options.
   * 
   * @param args From the command line.
   * @throws ParseException If found problem.
   */
  public void parse(String[] args) throws ParseException {
    CommandLineParser parser = new PosixParser();
    options.addOption("v",
                      "verbose", 
                      false, 
                      "Shows exception stack trace for errors.");
    
    Option uhm = new Option(uhmoption, 
                            false, 
                            "Site options for University of Hawaii at Manoa library" +
                            "where <id> is the UH id number in 1234-5678 or 12345678 format" +
                            "and <lastname> is the last name of the user");
    uhm.setArgs(2);
    uhm.setArgName("id> <lastname");
    options.addOption(uhm);
    
    Option hsl = new Option(hsloption, 
                            false, 
                            "Site options for Hawaii State Library(HSL)" +
                            "where <id> is the HSL id number" +
                            "and <lastfourdigit> is the last four digits of phone number.");
    hsl.setArgs(2);
    hsl.setArgName("id> <lastname");
    options.addOption(hsl);
    
    commandLine = parser.parse(options, args);
    
    if (!this.hasUhmOption() && !this.hasHslOption()) {
      throw new ParseException("no -uhm or -hsl used."); 
    }
  }
 
  /**
   * Check to see if -verbose was used.
   * 
   * @return true if used, false otherwise.
   */
  public boolean hasVerboseOption() {
    return commandLine.hasOption("verbose");
  }
  
  /**
   * Checks if -uhm was used.
   * 
   * @return true if used, false if not used
   */
  public boolean hasUhmOption() {
    return commandLine.hasOption(uhmoption);
  }
  
  /**
   * Get the value of -uhm id option.
   * 
   * @return Value of -uhm id option.
   */
  public String getUhmId() {
    String[] values = commandLine.getOptionValues(uhmoption);
    return values[0];
  }
  
  /**
   * Get the value of -uhm lastName option.
   * 
   * @return Value of -uhm lastName.
   */
  public String getUhmLastName() {
    String[] values = commandLine.getOptionValues(uhmoption);
    return values[1];
  }
  
  /**
   * Checks if -within was used.
   * 
   * @return true if -within used, false if -within not used
   */
  public boolean hasHslOption() {
    return commandLine.hasOption(hsloption);
  }
  
  /**
   * Get option1 of -hsl option.
   * 
   * @return The value of option1 for -hsl.
   */
  public String getHslId() {
    String[] values = commandLine.getOptionValues(hsloption);
    return values[0];
  }
  /**
   * Get option2 of -hsl option.
   * 
   * @return The value of option2 for -hsl.
   */
  public String getHslLastFourDigitsForPhoneNumber() {
    String[] values = commandLine.getOptionValues(hsloption);
    return values[1];
  }
  
  /**
   * Checks if -within was used.
   * 
   * @return true if -within used, false if -within not used
   */
  public boolean hasSortOption() {
    return commandLine.hasOption("sort");
  }
  
  /**
   * Get the -sort value.
   * 
   * @return either duedates or library (default: library)
   */
  public String getSortValue() {
    if (!this.hasSortOption()) {
      return "duedates";
    }
    String[] values = commandLine.getOptionValues("sort");
    return values[0];
  }
  
  /**
   * Checks if -within was used.
   * 
   * @return true if -within used, false if -within not used
   */
  public boolean hasWithinOption() {
    return commandLine.hasOption("within");
  }

  /**
   * Get the value of -within option.
   * @return Value of -within option (default: 0).
   */
  public int getWithinValue() {
    if (!this.hasWithinOption()) {
      return 0;
    }
    String[] values = commandLine.getOptionValues("within");
    return Integer.parseInt(values[0]);
  }

  /**
   * Print the command line help information.
   */
  public void printHelp() {
    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp("java -jar DueDates.jar [-verbose]" +
                        " [-sort <library|duedate>] [-within <days>]" +
                        " -uhm <id> <last name> | -hsl <barcode> <pin>", options);
  }
  
  /** */
  private CommandLine commandLine = null;
  private final Options options = new Options();
}
